using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

using Jamila2.Tools;

public partial class GonzalesMain : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region MessageNoApproverRequired

    protected string MessageNoApproverRequired
    {
        get
        {
            return ViewState["MessageNoApproverRequired"].ToString();
        }

        set
        {
            ViewState["MessageNoApproverRequired"] = value;
        }
    }

    #endregion

    #region MessageNoReportingPersonnelSetup

    protected string MessageNoReportingPersonnelSetup
    {
        get
        {
            return ViewState["MessageNoReportingPersonnelSetup"].ToString();
        }

        set
        {
            ViewState["MessageNoReportingPersonnelSetup"] = value;
        }
    }

    #endregion

    #region MessageNoApproverMessage

    protected string MessageNoApproverMessage
    {
        get
        {
            return ViewState["MessageNoApproverMessage"].ToString();
        }

        set
        {
            ViewState["MessageNoApproverMessage"] = value;
        }
    }

    #endregion

    #region MessageNoFinanceVerifier

    protected string MessageNoFinanceVerifier
    {
        get
        {
            return ViewState["MessageNoFinanceVerifier"].ToString();
        }

        set
        {
            ViewState["MessageNoFinanceVerifier"] = value;
        }
    }

    #endregion

    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();    // first thing to do for every page

        if (Session[GonzalesCommonNamingConvention.CurrentRedirectSession] != null)
        {
            string redirect = Session[GonzalesCommonNamingConvention.CurrentRedirectSession].ToString();
            Session.Remove(GonzalesCommonNamingConvention.CurrentRedirectSession);
            /*
            string js_redirect = "<script language='javascript'>";
            js_redirect += "alert('zzz');";
            js_redirect += "<";
            js_redirect += "/";
            js_redirect += "script>";

            this.RegisterClientScriptBlock("redirect", js_redirect);
            */
            Server.Transfer(redirect);
        }

        if (!IsPostBack)
        {
            this.SwitchLanguage();
            this.LoadData();
        }

    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {        

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }
        
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");

                this.Label_AD.Text = CurLanguageFactory.DisplayValue("ItemADID");
                this.Label_Approver.Text = CurLanguageFactory.DisplayValue("ItemApprover");
                this.Label_Company.Text = CurLanguageFactory.DisplayValue("ItemCompany");
                this.Label_DateFormat.Text = CurLanguageFactory.DisplayValue("ItemDateFormat");
                this.Label_Entity.Text = CurLanguageFactory.DisplayValue("ItemEntity");
                this.Label_FinanceVerifier.Text = CurLanguageFactory.DisplayValue("ItemFinanceVerifier");
                //this.Label_Language.Text = CurLanguageFactory.DisplayValue("ItemLanguage");
                this.Label_Name.Text = CurLanguageFactory.DisplayValue("ItemName");
                this.Label_ReportingTo.Text = CurLanguageFactory.DisplayValue("ItemReportingTo");

                this.MessageNoApproverMessage = CurLanguageFactory.DisplayValue("NoApproverMessage");
                this.MessageNoApproverRequired = CurLanguageFactory.DisplayValue("NoApproverRequired");
                this.MessageNoFinanceVerifier = CurLanguageFactory.DisplayValue("NoFinanceVerifier");
                this.MessageNoReportingPersonnelSetup = CurLanguageFactory.DisplayValue("NoReportingPersonnelSetup");

                this.Label_Email.Text = CurLanguageFactory.DisplayValue("ItemEmail");
                this.Label_CreditCardNo.Text = CurLanguageFactory.DisplayValue("ItemCreditCardNo");
                this.Label_ApprovingLimit.Text = CurLanguageFactory.DisplayValue("ItemApprovingLimit");
                this.Label_AssistantClaimant.Text = CurLanguageFactory.DisplayValue("ItemAssistantClaimant");
                this.Label_AssistantReviewer.Text = CurLanguageFactory.DisplayValue("ItemAssistantReviewer");

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesMain.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region LoadData

    public void LoadData()
    {
        // reporting
        if (this.CurrentPrivateSession.CurrentLoggedOnUser.PersonnelReportTo != null)
        {
            this.LabelValue_ReportingTo.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.PersonnelReportTo.DisplayName;
        }
        else
        {
            this.LabelValue_ReportingTo.Text = this.MessageNoReportingPersonnelSetup;
            this.LabelValue_ReportingTo.CssClass = "MessageError";
        }

        // approver
        SystemUser Approver = null;

        if (this.CurrentPrivateSession.CurrentLoggedOnUser.NoApprover)
        {
            this.LabelValue_Approver.Text = this.MessageNoApproverRequired;
        }
        else
        {
            if (this.CurrentPrivateSession.CurrentLoggedOnUser.AppRoute == ApprovingRoute.Normal)
            {
                Approver = this.CurrentPrivateSession.CurrentLoggedOnUser.PersonnelReportTo;
            }
            else if (this.CurrentPrivateSession.CurrentLoggedOnUser.AppRoute == ApprovingRoute.MultiLevel)
            {
                Approver = this.CurrentPrivateSession.CurrentLoggedOnUser.PersonnelReportTo;
            }
            else if (this.CurrentPrivateSession.CurrentLoggedOnUser.AppRoute == ApprovingRoute.Exception)
            {
                using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
                {
                    SystemUsers Approvers = CurUserManager.GetExceptionalApproversMatrix(this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID);
                    if (Approvers != null && Approvers.Count > 0)
                    {
                        Approver = Approvers[0];
                    }
                }
            }

            if (Approver != null)
            {
                this.LabelValue_Approver.Text = Approver.DisplayName;
            }
            else
            {
                this.LabelValue_Approver.Text = this.MessageNoReportingPersonnelSetup;
                this.LabelValue_Approver.CssClass = "MessageError";
            }
        }

        // finance verifier
        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        {
            SystemUsers FinanceVerifiers = CurUserManager.GetFinanceAdminByCompany(this.CurrentPrivateSession.CurrentLoggedOnUser.FromCompany.InternalID);

            if (FinanceVerifiers != null && FinanceVerifiers.Count > 0)
            {
                for (int x = 0; x < FinanceVerifiers.Count; x++)
                {
                    this.LabelValue_FinanceVerifier.Text += FinanceVerifiers[x].DisplayName + ", ";
                }

                this.LabelValue_FinanceVerifier.Text = this.LabelValue_FinanceVerifier.Text.Substring(0, (this.LabelValue_FinanceVerifier.Text.Length - 2));
            }
            else
            {
                this.LabelValue_FinanceVerifier.Text = this.MessageNoFinanceVerifier;
            }
        }

        
        this.LabelValue_Name.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName;
        //this.LabelValue_Language.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.DisplayName;
        this.LabelValue_DateFormat.Text = SwissArmy.DateFormatForDisplay(this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/");
        this.LabelValue_ADName.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.EmailID;
        this.LabelValue_Company.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.FromCompany.CompanyName;
        this.LabelValue_Entity.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.FromCompany.Entity.DisplayName;

        //newly added by Agnes[10 Jul 2009]
        this.LabelValue_Email.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.Email;
        this.LabelValue_CreditCardNo.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.CreditCardNo;

        if(this.CurrentPrivateSession.CurrentLoggedOnUser.ApprovingLimit > 0)
            this.LabelValue_ApprovingLimit.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.ApprovingCurrency.DisplayName + " " + this.CurrentPrivateSession.CurrentLoggedOnUser.ApprovingLimit.ToString();

        if (this.CurrentPrivateSession.CurrentLoggedOnUser.OnBehalfBy != null)
        {
            //this.LabelValue_AssistantClaimant.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.OnBehalfBy.EmailID;
            using (UserManager curUserManager = new UserManager(this.CurrentPrivateSession))
            {
                SystemUser OnBehalfBy = curUserManager.GetUserByInternalID(this.CurrentPrivateSession.CurrentLoggedOnUser.OnBehalfBy.InternalID);
                this.LabelValue_AssistantClaimant.Text = OnBehalfBy.DisplayName;                
            }
        }

        if (this.CurrentPrivateSession.CurrentLoggedOnUser.ReviewOnBehalfBy != null)
        {
            //this.LabelValue_AssistantReviewer.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.ReviewOnBehalfBy.EmailID;
            using (UserManager curUserManager = new UserManager(this.CurrentPrivateSession))
            {
                SystemUser ReviewOnBehalf = curUserManager.GetUserByInternalID(this.CurrentPrivateSession.CurrentLoggedOnUser.ReviewOnBehalfBy.InternalID);
                this.LabelValue_AssistantReviewer.Text = ReviewOnBehalf.DisplayName;
            }
        }

        // shadow
        if (this.CurrentPrivateSession.ShadowMode)
        {
            this.LabelValue_Name.Text += " (Login by " + this.CurrentPrivateSession.ShadowUser.DisplayName + ")";
        }
    }

    #endregion

    #endregion
}
